package com.smartcruise.task;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 * 任务adapter
 */

public class NewTaskAdapter extends ArrayAdapter<CruiseData> {

    private Calendar calendar;

    public NewTaskAdapter(@NonNull Context context, @NonNull List<CruiseData> objects, Calendar calendar) {
        super(context, R.layout.item_main_task, objects);
        this.calendar=calendar;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_main_task, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final CruiseData cruiseData = getItem(position);
        viewHolder.taskName.setText(cruiseData.getName());
        viewHolder.taskType.setText(cruiseData.getType());
        viewHolder.time.setText("预耗时:" + cruiseData.getCostTime() + "小时");

        if (!cruiseData.isCopyData()) {
            viewHolder.taskStatus.setText("");
        } else {
            if (CruiseContract.TASK_IS_END.equals(cruiseData.getStatusInApp())) {//结束的任务显示上传状态
                switch (cruiseData.getUploadStatus()) {
                    case 0:
                        viewHolder.taskStatus.setText("待上传");
                        break;
                    case 1:
                        viewHolder.taskStatus.setText("已上传概要数据");
                        break;
                    case 2:
                        viewHolder.taskStatus.setText("已上传全部数据");
                        break;
                }
            } else {
                viewHolder.taskStatus.setText(cruiseData.getStatusInApp());
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaskWithMapActivity.start(getContext(), cruiseData.getId(),calendar);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.taskName)
        TextView taskName;
        @BindView(R.id.taskType)
        TextView taskType;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.taskStatus)
        TextView taskStatus;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
